// JavaScript Document
var xml = loadXMLDoc("index_files/docs.xml");

window.onload = load;

function load() {
	updateTitle();
	updateSider();
	var initName=loadFirstPath();
	var height = document.documentElement.clientHeight;
	height -= document.getElementById('myBody').offsetTop;

	// not sure how to get this dynamically
	height -= 90; /* whatever you set your body bottom margin/padding to be */
	document.getElementById('myBody').style.height = height + "px";

	var p = document.getElementById('mainContent');
	p.innerHTML = "<object id=\"content\" data=" + initName
			+ ".html type=\"text\html\"/>";
	refreshPDFLink();
	changeType("text/html")
}

function refreshPDFLink() {
	var name = getName();
	document.getElementById('toPdf').href = name + ".pdf";
}

function changeType(type) {
	if (type == document.getElementById('content').type)
		return;
	var name = getName();
	if (type == "text/html") {
		name = name + ".html";
	} else {
		name = name + ".pdf";
	}

	var p = document.getElementById('mainContent');

	removeNode(document.getElementById('content'));

	p.innerHTML = "<object id=\"content\" data=" + name + " type=\"" + type
			+ "\" />";

}

function changeDoc(name) {
	var oldName = getName();
	var type = document.getElementById('content').type;
	if (oldName == name)
		return;
	if (type == "text/html") {
		name = name + ".html";
	} else {
		name = name + ".pdf";
	}
	var p = document.getElementById('mainContent');
	removeNode(document.getElementById('content'));
	p.innerHTML = "<object id=\"content\" data=" + name + " type=\"" + type
			+ "\" />";
	refreshPDFLink();
}

function getName() {
	var name = document.getElementById('content').data;
	name = decodeURI(name);
	name = name.substr(0, name.lastIndexOf('.'));
	return name;
}
function removeNode(node) {
	if (node)
		node.parentNode.removeChild(node);
}

function loadXMLDoc(fname) {
	var xmlDoc;
	// code for IE
	if (window.ActiveXObject) {
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
	}
	// code for Mozilla, Firefox, Opera, etc.
	else if (document.implementation && document.implementation.createDocument) {
		xmlDoc = document.implementation.createDocument("", "", null);
	} else {
		alert('Your browser cannot handle this script');
	}
	xmlDoc.async = false;
	xmlDoc.load(fname);
	return (xmlDoc);
}

function updateTitle(){
	var inHtml="";
	var title = xml.getElementsByTagName("title")[0];
	inHtml+= title.textContent;
	var p = document.getElementById('titleShow');
	p.innerHTML = inHtml;
}

function updateSider() {
	var inHtml="";
	var categorys = xml.getElementsByTagName("category");
	for ( var i = 0; i < categorys.length; i++) {
		var categoryName = categorys[i].getAttribute("name");
		inHtml += "<p><strong>" + categoryName + "</strong></p><ul>";
		var doc = first(categorys[i]);
		while (doc != null) {
			var docName = doc.getAttribute("name");
			var docPath = doc.getAttribute("path");
			inHtml += "<li><span class=\"btn\" onclick=\"changeDoc('" + docPath
					+ "')\">" + docName + "</span></li>";
			doc = next(doc);
		}
		inHtml += "</ul>"
	}
	var p = document.getElementById('sidebar1');
	p.innerHTML = inHtml;
}

function loadFirstPath(){
	var doc = xml.getElementsByTagName("doc")[0];
	var docPath = doc.getAttribute("path");
	return docPath;
}

function first(elem) {
	elem = elem.firstChild;
	return elem && elem.nodeType != 1 ? next(elem) : elem;
}

function next(elem) {
	do {
		elem = elem.nextSibling;
	} while (elem && elem.nodeType != 1);
	return elem;
}
